/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.pacman.form;

import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import sg.edu.nus.iss.pacman.dao.TopScoreUser;
import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 * A form to view the high score.
 * @author daniel, fredy
 */
public class ViewHighScoreForm extends List implements CommandListener
{
    private PacManMIDlet mainApp;
    private Command cmdOK;
    private Command cmdBack;
    private static final String TITLE = "High Score List";
    private TopScoreUser[] users;

    public ViewHighScoreForm(PacManMIDlet mainApp, TopScoreUser[] users)
    {
        super(TITLE, List.IMPLICIT, new String[] {}, null);
        this.mainApp = mainApp;
        this.users = users;
        init();
    }

    private void init()
    {
        cmdOK = new Command(PacManMIDlet.LABEL_CMD_OK, Command.OK, 1);
        cmdBack = new Command(PacManMIDlet.LABEL_CMD_BACK, Command.BACK, 1);
        addCommand(cmdOK);
        addCommand(cmdBack);
        this.setCommandListener(this);

        for (int i = 0; i < users.length; i++)
        {
            String line = "";
            if (users.length > 1)
            {
                line = "\n";
            }
            line = users[i].getUserName() + "  " +
                users[i].getMazeName() + "  " +
                users[i].getScore();
            insert(i, line, null);
        }

    }

    public void commandAction(Command c, Displayable d)
    {
        if (cmdOK == c)
        {
            int idx = getSelectedIndex();
            if (idx >= 0)
            {
                mainApp.displayHighScoreUserProfile(users[idx].getUserName());
            }
        }
        else if (cmdBack == c)
        {
            mainApp.displayMainMenu();
        }
    }
}
